﻿using System.Security.Cryptography;
using System.Text;

namespace Tszy.Unlimited.Base.Extensions;

/// <summary>
/// 字符串加密扩展。
/// </summary>
public static class EncryptExtensions
{
    #region MD5

    /// <summary>
    /// 将当前字符串转换成 32 位 MD5 加密。
    /// </summary>
    /// <param name="target">当前字符串。</param>
    /// <returns>32 位 MD5 加密后的字符串。</returns>
    public static string MD5(this string target) => target.MD5("", Encoding.UTF8);

    /// <summary>
    /// 将当前字符串转换成 32 位 MD5 加密。
    /// </summary>
    /// <param name="target">当前字符串。</param>
    /// <param name="key">密钥。</param>
    /// <param name="encoding">编码格式。</param>
    /// <returns>32 位 MD5 加密后的字符串。</returns>
    public static string MD5(this string target, string key, Encoding encoding)
    {
        target += key;

        var sb = new StringBuilder();
        using (MD5 md5 = System.Security.Cryptography.MD5.Create())
        {
            byte[] bytes = encoding.GetBytes(target);
            byte[] md5Bytes = md5.ComputeHash(bytes);
            for (int i = 0; i < md5Bytes.Length; i++)
            {
                sb.Append(md5Bytes[i].ToString("X2"));
            }
        }

        return sb.ToString().ToLower();
    }

    #endregion MD5

    #region 32 位 AES 加密解密

    /// <summary>
    /// AES 加密。
    /// </summary>
    /// <param name="text"></param>
    /// <param name="password"></param>
    /// <returns></returns>
    public static string AES32Encrypt(this string text, string password = "9D4#!uCb4%&83FO4")
    {
        byte[] plainText = Encoding.UTF8.GetBytes(text);

        var aes = Aes.Create();

        aes.Key = Encoding.UTF8.GetBytes(password);

        var cipherBytes = aes.EncryptEcb(plainText, PaddingMode.PKCS7);

        string result = string.Empty;
        //逐字节变为16进制字符
        for (int i = 0; i < cipherBytes.Length; i++)
        {
            result += cipherBytes[i].ToString("X2");
        }

        return result;
    }

    /// <summary>
    /// AES 解密。
    /// </summary>
    /// <param name="text"></param>
    /// <param name="password"></param>
    /// <returns></returns>
    public static string AES32Decrypt(this string text, string password = "9D4#!uCb4%&83FO4")
    {
        //byte[] encryptedData = Encoding.UTF8.GetBytes(text);
        byte[] encryptedData = new byte[text.Length / 2];
        for (int i = 0; i < encryptedData.Length; i++)
        {
            try
            {
                // 每两个字符是一个 byte。
                encryptedData[i] = byte.Parse(text.Substring(i * 2, 2),
                System.Globalization.NumberStyles.HexNumber);
            }
            catch
            {
                // Rethrow an exception with custom message.
                throw new ArgumentException("hex is not a valid hex number!", nameof(text));
            }
        }

        var aes = Aes.Create();

        aes.Key = Encoding.UTF8.GetBytes(password);

        var plainText = aes.DecryptEcb(encryptedData, PaddingMode.PKCS7);

        return Encoding.UTF8.GetString(plainText);
    }

    #endregion 32 位 AES 加密解密

    /// <summary>
    /// 
    /// </summary>
    /// <param name="target"></param>
    /// <returns></returns>
    public static string SHA256(this string target)
    {
        return Convert.ToBase64String(new SHA256Managed().ComputeHash(Encoding.UTF8.GetBytes(target)));
    }
}